using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class TitanTargetDetector : MonoBehaviour
{
	public static HashSet<Mortal> otherTitans = new HashSet<Mortal>();

	[Header("Ширина зоны захвата")]
	public float maxAimZoneWidth = 300f;

	public float minAimZoneWidth = 100f;

	public bool fixedMaxZoneWidth;

	[Header("Диапазон расстояний для изменения ширины зоны захвата")]
	public float maxDistanceAimZone = 300f;

	public float minDistanceAimZone = 10f;

	public Mortal nearestEnemy;

	[Range(1f, 10f)]
	public float coef = 1f;

	public bool enemyCatchedForHomingMissle;

	public TitanBehavior myTitan;

	private float _factorTimeofGoalCapture = 0.5f;

	private Coroutine watchEnemyIE;

	private RaycastHit cameraRayHit;

	private Vector3 aimHitPoint;

	public float homingMissleFlyHeight = 20f;

	public Vector3 highestMisslePosition;

	public static void InsertToTargetList(Mortal target)
	{
		otherTitans.Add(target);
	}

	public static void RemoveFromTargetList(Mortal target)
	{
		otherTitans.Remove(target);
	}

	public void StartWatchEnemyTitans()
	{
		if (watchEnemyIE == null)
		{
			watchEnemyIE = StartCoroutine(WatchEnemyTitansIE());
		}
	}

	public void StopWatchEnemyTitans()
	{
		if (watchEnemyIE != null)
		{
			StopCoroutine(watchEnemyIE);
			watchEnemyIE = null;
		}
		if (TitanfallControllerUI.instance != null && myTitan.isMine)
		{
			TitanfallControllerUI.instance.DisableAllInterface();
		}
	}

	private IEnumerator WatchEnemyTitansIE()
	{
		if (TitanfallControllerUI.instance == null || !myTitan.isMine)
		{
			yield break;
		}
		nearestEnemy = null;
		Mortal lastEnemy = nearestEnemy;
		while (true)
		{
			yield return null;
			if (otherTitans.Count == 0)
			{
				TitanfallControllerUI.instance.DisableAllInterface();
				continue;
			}
			if (myTitan == null)
			{
				TitanfallControllerUI.instance.DisableAllInterface();
				continue;
			}
			float dist;
			nearestEnemy = GetNearestEnemy(lastEnemy, out dist);
			TitanfallControllerUI.instance.UpdateFocusOnTarget(nearestEnemy, dist, myTitan, nearestEnemy != lastEnemy);
			lastEnemy = nearestEnemy;
		}
	}

	private Mortal GetNearestEnemy(Mortal lastEnemy, out float distance)
	{
		distance = float.MaxValue;
		Mortal mortal = null;
		foreach (Mortal otherTitan in otherTitans)
		{
			if (otherTitan == null || !otherTitan.enabled || otherTitan.isDead || (PhotonNetwork.connected && PlayerBehavior.IsTeammate(GameController.instance.FindPlayer(otherTitan.photonView.ownerId))) || (otherTitan is PlayerBehavior && ((otherTitan as PlayerBehavior).inCar || (otherTitan as PlayerBehavior).inHelic)))
			{
				continue;
			}
			Vector3 position = otherTitan.transform.position;
			if (TitanfallControllerUI.instance.TargetIsOnScreen(position))
			{
				float num = Vector3.Distance(position, myTitan.transform.position);
				float num2 = GetAimZoneWidthForTarget(num);
				if (otherTitan.Equals(lastEnemy) && !fixedMaxZoneWidth)
				{
					num2 = TitanfallControllerUI.instance.GetAimZoneWidthForLastTarget(num2, position);
				}
				if (num < distance && Mathf.Abs(TitanfallControllerUI.instance.PositionOnScreen(position).x) < num2)
				{
					mortal = otherTitan;
					distance = num;
				}
			}
		}
		if (mortal != null && !mortal.Equals(lastEnemy))
		{
			SetDelayAimValues(mortal);
		}
		return mortal;
	}

	private void SetDelayAimValues(Mortal nearestTarget)
	{
		Vector3 positionForChargeHit = nearestTarget.GetPositionForChargeHit();
		Vector3 direction = positionForChargeHit - myTitan.topTitanPart.transform.position;
		float num = 1f;
		RaycastHit hitInfo;
		if (Physics.Raycast(myTitan.topTitanPart.transform.position, direction, out hitInfo, 1000f, myTitan.collisionLayer) && hitInfo.collider.gameObject.layer != LayerMask.NameToLayer("Car") && hitInfo.collider.gameObject.layer != LayerMask.NameToLayer("PlayerLayer"))
		{
			num += _factorTimeofGoalCapture;
		}
		TitanfallControllerUI.instance.CatchAlpha.offset = TitanfallControllerUI.instance.DefaultCatchAlpahOffset * num;
		TitanfallControllerUI.instance.FocusScale.time = TitanfallControllerUI.instance.DefaultFocusScaleTime * num;
	}

	public Vector3 GetAutoPointForShoot(bool mainWeapon, out bool homingMissleCatchEnemy)
	{
		homingMissleCatchEnemy = false;
		if (nearestEnemy == null)
		{
			return myTitan.topTitanPart.transform.position + myTitan.topTitanPart.transform.forward * 100f;
		}
		float num = Vector3.Distance(nearestEnemy.transform.position, myTitan.transform.position);
		bool flag = false;
		flag = ((!mainWeapon) ? myTitan.additionalWeaponsIsHoming : myTitan.mainWeaponsIsHoming);
		bool flag2 = false;
		if (flag && (num < myTitan.MainWeaponsMaxDistance() || num < myTitan.AdditionalWeaponsMaxDistance()))
		{
			homingMissleCatchEnemy = true;
			return nearestEnemy.GetPositionForChargeHit();
		}
		bool flag3 = false;
		if (!flag && (num < myTitan.MainWeaponsMaxDistance() || num < myTitan.AdditionalWeaponsMaxDistance()))
		{
			Camera mainCamera = TitanfallControllerUI.instance.mainCamera;
			Vector3 position = TitanfallControllerUI.instance.aimSprite.transform.position;
			Vector3 position2 = UICamera.currentCamera.WorldToScreenPoint(position);
			Ray ray = mainCamera.ScreenPointToRay(position2);
			if (Physics.Raycast(ray.origin + ray.direction * 30f, ray.direction, out cameraRayHit, 1000f, myTitan.collisionLayer))
			{
				aimHitPoint = cameraRayHit.point;
			}
			else
			{
				aimHitPoint = ray.origin + ray.direction * 1000f;
			}
			return aimHitPoint;
		}
		return myTitan.topTitanPart.transform.position + myTitan.topTitanPart.transform.forward * 1000f;
	}

	public float GetAimZoneWidthForTarget(float distanceToTarget)
	{
		float result = Screen.width;
		if (maxAimZoneWidth > 0f && minAimZoneWidth > 0f)
		{
			float num = Mathf.Clamp(distanceToTarget, minDistanceAimZone, maxDistanceAimZone);
			float num2 = (num - minDistanceAimZone) / (maxDistanceAimZone - minDistanceAimZone);
			result = num2 * (maxAimZoneWidth - minAimZoneWidth) + minAimZoneWidth;
		}
		return result;
	}

	public Quaternion GetQuaternionXToTarget()
	{
		if (nearestEnemy == null)
		{
			return Quaternion.identity;
		}
		Quaternion quaternion = Quaternion.LookRotation(nearestEnemy.GetPositionForChargeHit() - myTitan.topTitanPart.position);
		Debug.DrawLine(myTitan.topTitanPart.position, nearestEnemy.GetPositionForChargeHit(), Color.blue);
		Debug.DrawLine(myTitan.topTitanPart.position, myTitan.topTitanPart.right * Vector3.Distance(nearestEnemy.GetPositionForChargeHit(), myTitan.GetPositionForChargeHit()), Color.red);
		float x = quaternion.eulerAngles.x;
		return Quaternion.Euler(x, 0f, 0f);
	}
}
